package com.yeshine.opendata.controller.front.my.org;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.SysDictionary;
import com.yeshine.opendata.entity.Org;
import com.yeshine.opendata.service.IDictionaryService;
import com.yeshine.opendata.service.IOrgService;
import com.yeshine.opendata.service.IUserService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;
import com.yeshine.opendata.utils.mybatis.DashboardMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by taohj on 2014/7/7.
 */

@Controller
@RequestMapping("/front/my/org")
public class MyOrgController extends AbstractFrontControllerBase {


    @Autowired
    private IOrgService service;


    @Autowired
    private IUserService userService;

    @Autowired
    private IDictionaryService dictionaryService;


    @RequestMapping(value = "createV", method = RequestMethod.GET)
    public String createV() {
        Result<Org> result = service.getList(null);
        setRequestAttr("result", result);
        SysDictionary sysDictionary = new SysDictionary();
        sysDictionary.setType("ORG_CATEGORY");
        setRequestAttr("categorys", dictionaryService.getList(sysDictionary).getList());


        List<DashboardMenu> dashboardMenus = new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("新增机构", true));
        setRequestAttr("dashboardMenus", dashboardMenus);
        return "/front/my/org/create";
    }

    @RequestMapping(value = "create")
    public String create(
            @RequestParam("name") String name,
            @RequestParam(value = "superOrg", required = false) Integer superOrg,
            @RequestParam("image") MultipartFile image,
            @RequestParam("category") Integer category,
            @RequestParam("contacts") String contacts,
            @RequestParam("telephone") String telephone,
            @RequestParam("email") String email,
            @RequestParam("address") String address,
            @RequestParam("postcode") String postcode,
            @RequestParam("description") String description
    ) {
        Org org = new Org();
        org.setName(name);
        org.setHeadImageFile(image);
        org.setCategory(new SysDictionary(category));
        org.setContacts(contacts);
        org.setTelephone(telephone);
        org.setEmail(email);
        org.setAddress(address);
        org.setPostcode(postcode);

        org.setCreater(getCurrentUser());
        org.setCreateTime(new Date());
        if (superOrg != null) {
            org.setSuperOrg(new Org(superOrg));
        }

        org.setDescription(description);
        org.setStatus(new SysDictionary(111));
        org.setAuditStatus(new SysDictionary(181));
        Result<Org> result = service.create(org, getRequest());
        if (result.isSuccess()) {
            return "redirect:/front/my/org/list";
        } else {
            return setMessage("创建失败", "创建组织失败，请联系管理员");
        }
    }


    @RequestMapping("remove")
    public String remove(@RequestParam("id") Integer id) {
        Result<Org> result = service.remove(new Org(id));
        return getReturnString(result, "redirect:/front/my/org/list", setMessage("删除失败", result.getMessage()));
    }


    @RequestMapping(value = "modifyV")
    public String modify(@RequestParam("id") Integer id) {
        Org org = service.getById(id).getTarget();
        List<Org> orgList = service.getList(org).getList();
        setRequestAttr("org", org);
        setRequestAttr("orgList", orgList);
        setRequestAttr("categorys", dictionaryService.getList(new SysDictionary("ORG_CATEGORY")).getList());


        List<DashboardMenu> dashboardMenus = new ArrayList<DashboardMenu>();
        dashboardMenus.add(new DashboardMenu("更新机构", true));
        setRequestAttr("dashboardMenus", dashboardMenus);
        return "/front/my/org/modify";
    }


    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam(value = "superOrg", required = false) Integer superOrg,
            @RequestParam("image") MultipartFile image,
            @RequestParam("category") Integer category,
            @RequestParam("contacts") String contacts,
            @RequestParam("telephone") String telephone,
            @RequestParam("email") String email,
            @RequestParam("address") String address,
            @RequestParam("postcode") String postcode,
            @RequestParam("description") String description
    ) {
        Org org = service.getById(id).getTarget();
        org.setAuditStatus(new SysDictionary(181));
        org.setStatus(new SysDictionary(111));
        org.setName(name);
        org.setHeadImageFile(image);
        org.setSuperOrg(new Org(superOrg));
        org.setCategory(new SysDictionary(category));
        org.setContacts(contacts);
        org.setTelephone(telephone);
        org.setEmail(email);
        org.setAddress(address);
        org.setPostcode(postcode);
        org.setDescription(description);

        Result<Org> result = service.modify(org, getRequest());
        return getReturnString(result, "redirect:/front/my/org/list", setMessage("修改失败", "信息修改失败，请联系管理员"));
    }


    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "size", required = false, defaultValue = "20") Integer size,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "order", required = false) String order,
            @RequestParam(value = "superOrg", required = false) Integer superOrg
    ) {
        PageResult<Org> pageResult = new PageResult<Org>(page,size);

        Org org = new Org();

        if (StringUtils.hasLength(name)) {
            org.setName(name);
        }

        if (superOrg != null) {
            org.setSuperOrg(new Org(superOrg));
        }

        if (StringUtils.hasLength(order)) {
            pageResult.setSortBy(order);
            pageResult.setSortColumn("org.name");
        }

        pageResult.setT(org);
        Result<Org> result = service.getByPage(pageResult);

        setRequestAttr("result", result);


        if (result.isSuccess()) {
            setRequestAttr("name", name);
            setRequestAttr("order", order);
            return "/front/my/org/list";
        } else {
            return setRequestForwordAttr("访问发生异常", "访问发生异常，请联系管理员", "/");
        }
    }






    /*--------------=====================机构成员管理=======================---------------------------- */


   /* @RequestMapping("member/list")
    public String memberslist(
            @RequestParam("org") Integer org
    ) {
        Org o = service.getById(org).getTarget();
        setRequestAttr("org", o);
        return "/front/my/org/member/list";

    }


    @RequestMapping("member/addV")
    public String addMemberV(

            @RequestParam(value = "page",required = false) Integer page,
            @RequestParam(value = "size",required = false) Integer size,
            @RequestParam(value = "id",required = false) Integer id,
            @RequestParam(value = "username",required = false) String username,
            @RequestParam(value = "sex",required = false) Integer sex,
            @RequestParam("org") Integer org
    ) {

        User user=new User();
        user.setId(id);
        user.setUsername(username);
        user.setSex(new Dictionary(sex));

        PageResult<User> pageResult=new PageResult<User>(page,size);
        pageResult.setT(user);
        pageResult.setWhere(" and user.id  not in ( select user from T_User_Org where org='"+org+"' ) ");
        Result<User> result=userService.getByPage(pageResult);
        setRequestAttr("result", result);
        setRequestAttr("org", org);
        return "/front/my/org/member/create";

    }


    @ResponseBody
    @RequestMapping("member/add")
    public String addMember(
            @RequestParam("org") Integer org,
            @RequestParam("users[]") Integer users[],
            @RequestParam(value = "role",required = false) Integer role
    ) {
        Result<Org> result = service.addMember(org, users, role);

        return result.toString();
        //return getReturnString(result, "/front/my/org/member/list", setMessage("操作失败", result.getMessage()));

    }

    @RequestMapping("member/modify")
    public String modifyMember(
            @RequestParam("org") Integer org,
            @RequestParam("users[]") Integer users[],
            @RequestParam("role") Integer role
    ) {
        Result<Org> result = service.modifyMember(org, users, role);
        return getReturnString(result, "/front/my/org/member/list", setMessage("操作失败", result.getMessage()));
    }


    @ResponseBody
    @RequestMapping("member/remove")
    public String modifyMember(
            @RequestParam("org") Integer org,
            @RequestParam("users[]") Integer users[]
    ) {
        Result<Org> result = service.removeMember(org, users);
        return  result.toString();
       // return getReturnString(result, "/front/my/org/member/list", setMessage("操作失败", result.getMessage()));

    }
*/

}

